home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: phcoms4.seri.philips.nl!philce!hartj
- From: hartj@ce.philips.nl (Joost 't Hart)
- Subject: Re: Syntax clarification
- Message-ID: <Dn33Ft.H9J@ce.philips.nl>
- Sender: usenet@ce.philips.nl (USENET News System)
- Nntp-Posting-Host: adcsun10
- Organization: Philips Consumer Electronics, Eindhoven, The Netherlands
- References: <31236966.1881@sisna.com>
- Date: Tue, 20 Feb 1996 17:04:36 GMT
-
- Matt Smolic <msmolic@sisna.com> writes:
-
- >I am just making the switch from C to C++. Something I have come across
- >and cannot find an answer to is the use of the "&" symbol. In C this
- >means the address of, (e.g.) scanf("%d", &Avariable) would return the
- >address of Avariable. In C++ I constantly see syntax such as, in
- >declaring a class, Complex pow(const Complex & c, const Complex & power)
- >What does the & symbol do here?
-
- Matt,
-
- One might as well have a peep in an arbitrary book on C++, nevertheless
- following might be of some quick'n dirty little help.
-
- The & - "symbol" (as you call it) in C is an OPERATOR, delivering the address
- of a variable, as in:
-
- int bla = 0;
- int* p_bla;
-
- p_bla = & bla;
-
- This operator usage of '&' is (of course) also useful and allowed in C++.
-
- C++ uses the & - "symbol" additionally as part of a TYPE (modifier), called
- "reference TYPE".
-
- int aap = 0;
- int& bla = aap;
-
- declares an integer reference - called bla - to aap. One can best think of
- such a reference as an ALIAS: where in the code is spoken of 'bla', one might
- as well speak of 'aap'.
-
- bla = 4;
-
- changes the value of aap to 4.
-
- Note that a reference - once created - shall ALWAYS refer to its original
- counterpart. Therefore it is always declared using an initializer, which is the
- case for the formal parameters in the function you mentioned.
-
- On implementation level in C the reference construct can thus best be compared
- to
-
- *p_bla = 4
-
- which also modifies the value of bla - via a back door.
-
- Clear?
- JtH
-